﻿/*------------------------------------------------------------------------

NONCXXLOBJECT.HPP v3.1

Copyright 楊志賢 CxxlMan, 2012
All Rights Reserved

用來包裹非 cxxlObject 的 class
------------------------------------------------------------------------*/
#if !defined(__NONCXXLOBJECT_HPP_CxxlMan)
#define __NONCXXLOBJECT_HPP_CxxlMan

#include "CXXLOBJECT.HPP"

namespace CxxlMan
{

// 內定 delete 物件的函數
template <typename T>
void cxxlFASTCALL non_cxxlObject_wrapper_default_delete(T *toDelObj)
{
  delete toDelObj;
}

template <typename T>
class non_cxxlObject_wrapper:virtual public cxxlObject
{
  void (cxxlFASTCALL *toDel)(T *); // 指定 delete 物件的函數
  T *m_Obj; // 包裹的非 cxxlObject 物件

public:
  // Constructor
  non_cxxlObject_wrapper(T *Obj,
    void (cxxlFASTCALL *FreeFunc)(T *)  = non_cxxlObject_wrapper_default_delete,
    ISpirit *spirit = Spirit_Easy)
    :cxxlObject(spirit)
  {
    m_Obj = Obj;
    toDel = FreeFunc;
  }

  // Destructor
  virtual ~non_cxxlObject_wrapper()
  {
    toDel(m_Obj);
  }

  // 可用像 (*ObjPtr)-> 的方式使用包裹的物件
  // ObjPtr 是 Smart_Ptr
  T *cxxlFASTCALL operator->() const
  {
    return m_Obj;
  }

};

}   /* namespace CxxlMan */
#endif
